From 74199a27a8d1a02afcb21014a5fe686ece375251 Mon Sep 17 00:00:00 2001 From: oliskoli Date: Fri, 5 Sep 2008 20:18:12 +0000 Subject: [PATCH] Remove initial unicode check (issues problems with piped statements). Now unicode header will be detected within 'gbfgetstr'. --- arcdist.c | 1 - compegps.c | 3 +-- cst.c | 3 +-- csv_util.c | 2 ++ g7towin.c | 3 +-- garmin_txt.c | 4 ++-- gbfile.c | 58 ++++++++++++++++++++------------------------------- gbfile.h | 1 - gopal.c | 3 ++- gpsutil.c | 6 ++++-- igc.c | 5 +++-- main.c | 1 - netstumbler.c | 3 ++- nmea.c | 3 ++- nmn4.c | 3 ++- overlay.c | 4 +++- ozi.c | 4 ++-- pcx.c | 4 +++- polygon.c | 1 - stmsdf.c | 3 +-- stmwpp.c | 4 +++- tiger.c | 3 ++- tmpro.c | 3 +-- unicsv.c | 3 +-- xcsv.c | 2 -- xmlgeneric.c | 1 - 26 files changed, 61 insertions(+), 70 deletions(-) diff --git a/arcdist.c b/arcdist.c index 3b9a87640..ecbc75db2 100644 --- a/arcdist.c +++ b/arcdist.c @@ -63,7 +63,6 @@ arcdist_process(void) gbfile *file_in; file_in = gbfopen(arcfileopt, "r", MYNAME); - (void) gbfunicode(file_in); /* check for unicode text file */ lat1 = lon1 = lat2 = lon2 = BADVAL; while ((line = gbfgetstr(file_in))) { diff --git a/compegps.c b/compegps.c index e33793ab9..cc4b2f90d 100644 --- a/compegps.c +++ b/compegps.c @@ -335,7 +335,6 @@ static void compegps_rd_init(const char *fname) { fin = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(fin)) cet_convert_init(CET_CHARSET_UTF8, 1); input_datum = DATUM_WGS84; } @@ -360,7 +359,7 @@ compegps_data_read(void) char *cin = buff; char *ctail; - line++; + if ((line++ == 0) && fin->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); cin = lrtrim(buff); if (strlen(cin) == 0) continue; diff --git a/cst.c b/cst.c index e22d43cc6..e58421b12 100644 --- a/cst.c +++ b/cst.c @@ -139,7 +139,6 @@ static void cst_rd_init(const char *fname) { fin = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(fin)) cet_convert_init(CET_CHARSET_UTF8, 1); temp_route = NULL; } @@ -169,7 +168,7 @@ cst_data_read(void) { char *cin = buff; - line++; + if ((line++ == 0) && fin->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); cin = lrtrim(buff); if (strlen(cin) == 0) continue; diff --git a/csv_util.c b/csv_util.c index 6a83ed70d..77a50024c 100644 --- a/csv_util.c +++ b/csv_util.c @@ -1258,6 +1258,8 @@ xcsv_data_read(void) } while ((buff = gbfgetstr(xcsv_file.xcsvfp))) { + if ((linecount == 0) && xcsv_file.xcsvfp->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); + linecount++; /* Whack trailing space; leading space may matter if our field sep * is whitespace and we have leading whitespace. diff --git a/g7towin.c b/g7towin.c index acf7cebe1..628d5a303 100644 --- a/g7towin.c +++ b/g7towin.c @@ -361,7 +361,6 @@ static void rd_init(const char *fname) { fin = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(fin)) cet_convert_init(CET_CHARSET_UTF8, 1); gardown = 1; mode = wptdata; @@ -390,7 +389,7 @@ data_read(void) char *cin = buff; char *cdata; - line++; + if ((line++ == 0) && fin->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); cin = lrtrim(buff); if (!*cin) continue; diff --git a/garmin_txt.c b/garmin_txt.c index 8ebe8df43..99748e96c 100644 --- a/garmin_txt.c +++ b/garmin_txt.c @@ -1213,7 +1213,6 @@ garmin_txt_rd_init(const char *fname) memset(>xt_flags, 0, sizeof(gtxt_flags)); fin = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(fin)) cet_convert_init(CET_CHARSET_UTF8, 1); memset(&header_ct, 0, sizeof(header_ct)); datum_index = -1; @@ -1244,7 +1243,8 @@ garmin_txt_read(void) while ((buff = gbfgetstr(fin))) { char *cin; - current_line++; + if ((current_line++ == 0) && fin->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); + cin = lrtrim(buff); if (*cin == '\0') continue; diff --git a/gbfile.c b/gbfile.c index 83a87dd73..bf450791b 100644 --- a/gbfile.c +++ b/gbfile.c @@ -804,7 +804,7 @@ gbfgetstr(gbfile *file) if (file->unicode) return gbfgetucs2str(file); for (;;) { - char c = gbfgetc(file); + int c = gbfgetc(file); if ((c == EOF) || (c == 0x1A)) { if (len == 0) { @@ -821,11 +821,32 @@ gbfgetstr(gbfile *file) else if (c == '\n') { break; } + else if (((c == 0xFE) || (c == 0xFF)) && (! file->unicode_checked)) { + int cx; + int c1 = gbfgetc(file); + if (c1 != EOF) { + cx = c | (c1 << 8); + if (cx == 0xFEFF) { + file->unicode = 1; + file->big_endian = 0; + return gbfgetucs2str(file); + } + else if (cx == 0xFFFE) { + file->unicode = 1; + file->big_endian = 1; + return gbfgetucs2str(file); + } + else gbfungetc(c1, file); + } + } + + file->unicode_checked = 1; + if (len == file->linesz) { file->linesz += 64; result = file->line = xrealloc(file->line, file->linesz + 1); } - result[len] = c; + result[len] = (char)c; len++; } result[len] = '\0'; // terminate resulting string @@ -929,38 +950,5 @@ gbfputpstr(const char *s, gbfile *file) return (len + 1); } -int -gbfunicode(gbfile *file) -{ - if (! file->unicode_checked) { - int c; - size_t pos; - - file->unicode_checked = 1; - - pos = gbftell(file); - gbfrewind(file); - - c = gbfgetc(file); - if (c == EOF) return 0; - - if ((c != 0xFE) && (c != 0xFF)) { - if (pos) gbfseek(file, pos, SEEK_SET); - return 0; - } - c = c | (gbfgetc(file) << 8); - - if (c == 0xFEFF) file->big_endian = 0; - else if (c == 0xFFFE) file->big_endian = 1; - else { - if (pos) gbfseek(file, pos, SEEK_SET); - return 0; - } - file->unicode = 1; - if (pos != 0) gbfseek(file, pos, SEEK_SET); - } - return file->unicode; -} - /* Thats all, sorry. */ diff --git a/gbfile.h b/gbfile.h index 662282ad9..d13e24a05 100644 --- a/gbfile.h +++ b/gbfile.h @@ -100,5 +100,4 @@ int gbfputflt(const float f, gbfile *file); // write a float value int gbfputcstr(const char *s, gbfile *file); // write string including '\0' int gbfputpstr(const char *s, gbfile *file); // write as pascal string -int gbfunicode(gbfile *file); #endif diff --git a/gopal.c b/gopal.c index 89175e265..2cdd43a69 100644 --- a/gopal.c +++ b/gopal.c @@ -126,7 +126,6 @@ gopal_rd_init(const char *fname) if (global_opts.debug_level > 1) fprintf(stderr,"setting minspeed to %5.1lf km/h and maxspeed to %5.1lf km/h\n",minspeed,maxspeed); fin = gbfopen(fname, "r", MYNAME); - if (gbfunicode(fin)) cet_convert_init(CET_CHARSET_UTF8, 1); memset(buff,0,sizeof(buff)); if (optdate) @@ -195,6 +194,8 @@ gopal_read(void) line=0; while ((buff = gbfgetstr(fin))) { + if ((line == 0) && fin->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); + str = buff = lrtrim(buff); if (*buff == '\0') continue; if (gopal_check_line(buff)!=8)continue; diff --git a/gpsutil.c b/gpsutil.c index ccd59c163..71e88dedc 100644 --- a/gpsutil.c +++ b/gpsutil.c @@ -30,7 +30,6 @@ static void rd_init(const char *fname) { file_in = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(file_in)) cet_convert_init(CET_CHARSET_UTF8, 1); } static void @@ -65,7 +64,7 @@ data_read(void) char alttype; char icon[3]; waypoint *wpt_tmp; - + int line = 0; /* * Make sure that all waypoints in single read have same * timestamp. @@ -76,6 +75,9 @@ data_read(void) while ((ibuf = gbfgetstr(file_in))) { int n, len; char *sn; + + if ((line++ == 0) && file_in->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); + /* A sharp in column zero or an blank line is a comment */ ibuf = lrtrim(ibuf); len = strlen(ibuf); diff --git a/igc.c b/igc.c index 64738509c..3fa95d518 100644 --- a/igc.c +++ b/igc.c @@ -30,6 +30,7 @@ static gbfile *file_in, *file_out; static char manufacturer[4]; static const route_head *head; static char *timeadj = NULL; +static int lineno; #define MYNAME "IGC" #define MAXRECLEN 79 // Includes null terminator and CR/LF @@ -93,6 +94,7 @@ static igc_rec_type_t get_record(char **rec) char *c; retry: *rec = c = gbfgetstr(file_in); + if ((lineno++ == 0) && file_in->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); if (c == NULL) return rec_none; len = strlen(c); @@ -112,8 +114,7 @@ static void rd_init(const char *fname) char *ibuf; file_in = gbfopen(fname, "r", MYNAME); - if (gbfunicode(file_in)) cet_convert_init(CET_CHARSET_UTF8, 1); - + lineno = 0; // File must begin with a manufacturer/ID record if (get_record(&ibuf) != rec_manuf_id || sscanf(ibuf, "A%3[A-Z]", manufacturer) != 1) { fatal(MYNAME ": %s is not an IGC file\n", fname); diff --git a/main.c b/main.c index 9109e67f7..6b0a2d99e 100644 --- a/main.c +++ b/main.c @@ -84,7 +84,6 @@ load_args(const char *filename, int *argc, char **argv[]) char **argv2; fin = gbfopen(filename, "r", "main"); - (void) gbfunicode(fin); /* check for unicode text file */ while ((str = gbfgetstr(fin))) { str = lrtrim(str); if ((*str == '\0') || (*str == '#')) continue; diff --git a/netstumbler.c b/netstumbler.c index f92d461ce..be29aca03 100644 --- a/netstumbler.c +++ b/netstumbler.c @@ -55,7 +55,6 @@ static void rd_init(const char *fname) { file_in = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(file_in)) cet_convert_init(CET_CHARSET_UTF8, 1); macstumbler = 0; } @@ -79,6 +78,7 @@ data_read(void) long flags = 0; int speed = 0, channel = 0; struct tm tm; + int line = 0; memset(&tm, 0, sizeof(tm)); @@ -86,6 +86,7 @@ data_read(void) char *field; int field_num, len, i, stealth = 0; + if ((line++ == 0) && file_in->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); ibuf = lrtrim(ibuf); /* A sharp in column zero might be a comment. Or it might be * something useful, like the date. diff --git a/nmea.c b/nmea.c index 6c28121dd..85ad6bfc5 100644 --- a/nmea.c +++ b/nmea.c @@ -287,7 +287,6 @@ nmea_rd_init(const char *fname) read_mode = rm_file; file_in = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(file_in)) cet_convert_init(CET_CHARSET_UTF8, 1); } static void @@ -987,6 +986,8 @@ nmea_read(void) line++; + if ((line == 0) & file_in->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); + if ((line == 0) && (case_ignore_strncmp(ibuf, "@SonyGPS/ver", 12) == 0)) { /* special hack for Sony GPS-CS1 files: they are fully (?) nmea compatible, but come with a header line like diff --git a/nmn4.c b/nmn4.c index 470c932ba..bb02c00be 100644 --- a/nmn4.c +++ b/nmn4.c @@ -101,6 +101,7 @@ nmn4_read_data(void) char *buff; char *str, *c; int column; + int line = 0; char *zip1, *zip2, *city, *street, *number; route_head *route; @@ -111,6 +112,7 @@ nmn4_read_data(void) while ((buff = gbfgetstr(fin))) { + if ((line++ == 0) && fin->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); str = buff = lrtrim(buff); if (*buff == '\0') continue; @@ -273,7 +275,6 @@ static void nmn4_rd_init(const char *fname) { fin = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(fin)) cet_convert_init(CET_CHARSET_UTF8, 1); } static void diff --git a/overlay.c b/overlay.c index 3c96f40a1..20b92674c 100644 --- a/overlay.c +++ b/overlay.c @@ -153,7 +153,6 @@ static void ovl_rd_init(char const *fname) { fpin = gbfopen(fname, "r", MYNAME); - if (gbfunicode(fpin)) cet_convert_init(CET_CHARSET_UTF8, 1); } #define SECTION_NONE 0 @@ -228,6 +227,7 @@ static void ovl_read(void) route_head *route_head = NULL; waypoint *wpt; int sym_cnt; + int lineno = 0; groups = NULL; groups_cnt = 0; @@ -240,6 +240,8 @@ static void ovl_read(void) isSection = SECTION_NONE; while ((line = gbfgetstr(fpin))) { + if ((lineno == 0) && fpin->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); + lineno++; line = lrtrim(line); if( (pstr = strstr(line,"[Symbol "))!= NULL) { diff --git a/ozi.c b/ozi.c index cb953f43f..04ebf4aff 100644 --- a/ozi.c +++ b/ozi.c @@ -394,7 +394,6 @@ static void rd_init(const char *fname) { file_in = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(file_in)) cet_convert_init(CET_CHARSET_UTF8, 1); mkshort_handle = mkshort_new_handle(); ozi_init_units(0); @@ -682,7 +681,8 @@ data_read(void) int linecount = 0; while ((buff = gbfgetstr(file_in))) { - linecount++; + + if ((linecount++ == 0) && file_in->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); /* * this is particularly nasty. use the first line of the file diff --git a/pcx.c b/pcx.c index 37ced9f21..6a4437b97 100644 --- a/pcx.c +++ b/pcx.c @@ -54,7 +54,6 @@ static void rd_init(const char *fname) { file_in = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(file_in)) cet_convert_init(CET_CHARSET_UTF8, 1); } static void @@ -99,6 +98,7 @@ data_read(void) char tbuf[20]; char nbuf[20]; int points; + int line = 0; read_as_degrees = 0; points = 0; @@ -108,6 +108,8 @@ data_read(void) char *ibuf = lrtrim(buff); char *cp; + if ((line++ == 0) && file_in->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); + switch (ibuf[0]) { case 'W': time[0] = 0; diff --git a/polygon.c b/polygon.c index 5a3d3a2eb..5f2abce18 100644 --- a/polygon.c +++ b/polygon.c @@ -259,7 +259,6 @@ polygon_process(void) gbfile *file_in; file_in = gbfopen(polyfileopt, "r", MYNAME); - (void) gbfunicode(file_in); /* check for unicode text file */ olat = olon = lat1 = lon1 = lat2 = lon2 = BADVAL; while ((line = gbfgetstr(file_in))) { diff --git a/stmsdf.c b/stmsdf.c index 3c0f7ebb9..db9881303 100644 --- a/stmsdf.c +++ b/stmsdf.c @@ -336,7 +336,6 @@ static void rd_init(const char *fname) { fin = gbfopen(fname, "r", MYNAME); - if (gbfunicode(fin)) cet_convert_init(CET_CHARSET_UTF8, 1); lineno = 0; route = NULL; @@ -364,7 +363,7 @@ data_read(void) while ((buf = gbfgetstr(fin))) { char *cin = lrtrim(buf); - lineno++; + if ((lineno++ == 0) && fin->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); if (*cin == '\0') continue; diff --git a/stmwpp.c b/stmwpp.c index 762fe21ce..14be1f683 100644 --- a/stmwpp.c +++ b/stmwpp.c @@ -61,7 +61,6 @@ static void stmwpp_rd_init(const char *fname) { fin = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(fin)) cet_convert_init(CET_CHARSET_UTF8, 1); track = NULL; route = NULL; wpt = NULL; @@ -77,6 +76,7 @@ static void stmwpp_data_read(void) { char *buff; + int line = 0; what = STM_NOTHING; buff = gbfgetstr(fin); @@ -91,6 +91,8 @@ stmwpp_data_read(void) int column = -1; struct tm time; + if ((line++ == 0) && fin->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); + buff = lrtrim(buff); if (*buff == '\0') continue; diff --git a/tiger.c b/tiger.c index f8f4b2040..e06c6212e 100644 --- a/tiger.c +++ b/tiger.c @@ -99,7 +99,6 @@ static void rd_init(const char *fname) { file_in = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(file_in)) cet_convert_init(CET_CHARSET_UTF8, 1); mkshort_handle = mkshort_new_handle(); } @@ -131,8 +130,10 @@ data_read(void) char icon[100]; char *ibuf; waypoint *wpt_tmp; + int line = 0; while ((ibuf = gbfgetstr(file_in))) { + if ((line++ == 0) && file_in->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); if( sscanf(ibuf, "%lf,%lf:%100[^:]:%100[^\n]", &lon, &lat, icon, desc)) { wpt_tmp = waypt_new(); diff --git a/tmpro.c b/tmpro.c index 8028123ac..c46899602 100644 --- a/tmpro.c +++ b/tmpro.c @@ -45,7 +45,6 @@ static void rd_init(const char *fname) { file_in = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(file_in)) cet_convert_init(CET_CHARSET_UTF8, 1); } static void @@ -77,7 +76,7 @@ data_read(void) int linecount = 0; while ((buff = gbfgetstr(file_in))) { - linecount++; + if ((linecount++ == 0) && file_in->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); /* skip the line if it contains "sHyperLink" as it is a header (I hope :) */ if ((strlen(buff)) && (strstr(buff, "sHyperLink") == NULL)) { diff --git a/unicsv.c b/unicsv.c index 15cc6d9ff..fc160664b 100644 --- a/unicsv.c +++ b/unicsv.c @@ -573,12 +573,11 @@ unicsv_rd_init(const char *fname) fin = gbfopen(fname, "rb", MYNAME); - if (gbfunicode(fin)) cet_convert_init(CET_CHARSET_UTF8, 1); - if ((c = gbfgetstr(fin))) unicsv_fondle_header(c); else unicsv_fieldsep = NULL; + if (fin->unicode) cet_convert_init(CET_CHARSET_UTF8, 1); } static void diff --git a/xcsv.c b/xcsv.c index 10e64862e..b72a26761 100644 --- a/xcsv.c +++ b/xcsv.c @@ -473,7 +473,6 @@ xcsv_read_style(const char *fname) xcsv_file_init(); fp = gbfopen(fname, "rb", MYNAME); - (void) gbfunicode(fp); while ((sbuff = gbfgetstr(fp))) { sbuff = lrtrim(sbuff); xcsv_parse_style_line(sbuff); @@ -546,7 +545,6 @@ xcsv_rd_init(const char *fname) } xcsv_file.xcsvfp = gbfopen(fname, "r", MYNAME); - if (gbfunicode(xcsv_file.xcsvfp)) cet_convert_init(CET_CHARSET_UTF8, 1); xcsv_file.gps_datum = GPS_Lookup_Datum_Index(opt_datum); is_fatal(xcsv_file.gps_datum < 0, MYNAME ": datum \"%s\" is not supported.", opt_datum); } diff --git a/xmlgeneric.c b/xmlgeneric.c index 6f82f2261..6fd18c0fc 100644 --- a/xmlgeneric.c +++ b/xmlgeneric.c @@ -315,7 +315,6 @@ xml_init0(const char *fname, xg_tag_mapping *tbl, const char *encoding, { if (fname) { ifd = gbfopen(fname, "r", MYNAME); - (void) gbfunicode(ifd); if (offset) { gbfseek(ifd, offset, SEEK_SET); } -- 2.30.2